package com.hualan.sfzy.controller;


import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.model.entity.CourseEvaluate;
import com.hualan.sfzy.service.CourseEvaluateService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

@RestController
@RequestMapping("/courseEvaluate")
public class CourseEvaluateController {
    @Autowired
    private CourseEvaluateService courseEvaluateService;
    @PostMapping()
    public Result add(@RequestBody CourseEvaluate courseEvaluate) {
     int i=     courseEvaluateService.add(courseEvaluate);
     if(i>0)
     {
         return Result.success();
     }
     return Result.error("添加失败");
    }
    @PutMapping()
    public Result update(@RequestBody CourseEvaluate courseEvaluate) {
        int i=courseEvaluateService.edit(courseEvaluate);
        if(i>0)
        {
            return Result.success();
        }
        return Result.error("修改失败");
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") int id) {
        int i=courseEvaluateService.remove(id);
        if(i>0)
        {
            return Result.success();
        }
        return Result.error("删除失败");
    }
    @DeleteMapping()
    public Result delete(@RequestParam Integer[] ids) {
        int i=courseEvaluateService.removeBatch(ids);
        if(i>0)
        {
            return Result.success();
        }
        return Result.error("删除失败");

    }
    @GetMapping("/all")
    public Result getAll(@RequestParam (value ="pageNum" ,defaultValue = CommonConstant.PAGE_NUM_DEFAULT)Integer pageNum,
                         @RequestParam (value = "pageSize" ,defaultValue = CommonConstant.PAGE_SIZE_DEFAULT)Integer pageSize,
                         CourseEvaluate courseEvaluate) {
        Map<String, Object> map = courseEvaluateService.findPage(pageNum, pageSize, courseEvaluate);
        return Result.success(map);


    }
    @GetMapping
    public Result get(@RequestParam Integer id) {
        CourseEvaluate courseEvaluate = courseEvaluateService.findById(id);

        return Result.success(courseEvaluate);
    }
    @GetMapping("/courseid")
    public Result getCourseid(@RequestParam Integer courseid) {
        int result=courseEvaluateService.findCourseEvaluateByCourseId(courseid);
        return Result.success(result);

    }
//    查询用户对课程评价是否存在
    @GetMapping("/evaluate")
    public Result getEvaluate(@RequestParam Integer userid,@RequestParam Integer courseid) {
        return Result.success(courseEvaluateService.findCourseEvaluateByCU(userid,courseid));
    }



}
